package com.hx.vending.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/26.
 */
public class SendRequest {

    /**
     * 发送连接请求获取流量卡信息
     * @param requestUrl 请求url
     * @param dataMap 请求体数据
     * @param requestMethod	请求的方法 POST/GET
     * @param authorization 账号密码加密后的
     * @return
     */
    public static Map<String, Object> SendFlowRequest(String requestUrl, Map dataMap, String requestMethod, String authorization) {
        Map<String, Object> resultMap = new HashMap<>();
        //发送请求
        OutputStreamWriter out = null;
        InputStream is = null;
        URL url;

        try {
            url = new URL(requestUrl);// 创建连接
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestMethod(requestMethod); // 设置请求方式

            String s = null;

            if (requestMethod.equals("POST")){
                connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
                connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
                if (ToolUtil.isNotEmpty(authorization)) {
                    connection.setRequestProperty("Authorization", "Basic "+authorization);//设置HTTP基本认证(Basic Authentication)
                }
                s = JSON.toJSON(dataMap).toString();
            }
            connection.connect();
            out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
            out.append(s);
            out.flush();
            out.close();

            // 读取响应
            is = connection.getInputStream();
            int length = (int) connection.getContentLength();// 获取长度
            if (length != -1) {
                byte[] data = new byte[length];
                byte[] temp = new byte[512];
                int readLen = 0;
                int destPos = 0;
                while ((readLen = is.read(temp)) > 0) {
                    System.arraycopy(temp, 0, data, destPos, readLen);
                    destPos += readLen;
                }
                String result = new String(data, "UTF-8"); // utf-8编码
                resultMap = (Map<String, Object>) JSONObject.parse(result.toString());
            }
        } catch (IOException e) {
//            e.printStackTrace();
            return resultMap;
        } finally {
            try {
                if (null != is && null != out){
                    out.close();
                    is.close();
                }
            } catch (IOException e) {
//                e.printStackTrace();
                return resultMap;
            }
        }
        return resultMap;
    }
}
